package Sort.sort2021;

/**
 * @author leiwenkang <leiwenkang@kuaishou.com>
 * Created on 2021-03-12
 */
public class Sort选择排序 {
    public static void main(String[] args) {
        int[] array = {3, 5, 7, 2, 1, 4, 8, 9, 6};
        sort(array);
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
    }

    public static void sort(int[] array) {
        if (array == null || array.length == 0) {
            return;
        }

        for (int i = 0; i < array.length - 1; i++) {
            int min = i;
            for (int j = i + 1; j < array.length; j++) {
                if (array[j] < array[min]) {
                    min = j;
                }
            }
            if (min != i) {
                exchange(array, min, i);
            }
        }
    }

    private static void exchange(int[] array, int i, int j) {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
}
